package com.ruoyi.web.controller.knowledgebase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.knowledgebase.domain.Knowledge;
import com.ruoyi.knowledgebase.service.KnowledgeService;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/KnowledgeController")
public class KnowledgeController extends BaseController {
    @Resource
    private KnowledgeService knowledgeService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }
    @Log(title = "知识库查询", businessType = BusinessType.QUERY)
    @PostMapping("/queryKnowledge")
    public AjaxResult queryKnowledge(@RequestBody Knowledge knowledge)
    {
        List<Knowledge> Knowledges = knowledgeService.queryKnowledge(knowledge);
        return success(Knowledges);
    }

    @Log(title = "知识库查询", businessType = BusinessType.QUERY)
    @GetMapping("/queryKnowledgeUp")
    public TableDataInfo queryKnowledgeUp(Knowledge knowledge)
    {
        knowledge.setState("1");
        startPage();
        List<Knowledge> Knowledges = knowledgeService.queryKnowledge(knowledge);
        return getDataTable(Knowledges);
    }



    @Log(title = "知识库删除", businessType = BusinessType.DELETE)
    @PostMapping("/deleteKnowledge")
    public AjaxResult deleteKnowledge(@RequestBody Knowledge knowledge)
    {
        boolean b = knowledgeService.deleteKnowledge(knowledge);
        if(b){
            return success("删除成功！");
        }else{
            return success("删除失败！");
        }

    }
    @Log(title = "知识库新增", businessType = BusinessType.INSERT)
    @PostMapping("/addKnowledge")
    public AjaxResult addKnowledge(@RequestBody Knowledge knowledge)
    {
        boolean b = knowledgeService.save(knowledge);
        if(b){
            return success("新增成功,交予管理员审核！");
        }else{
            return success("新增失败！");
        }

    }
    @Log(title = "知识库修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateKnowledge")
    public AjaxResult updateKnowledge(@RequestBody Knowledge knowledge)
    {
        boolean b = knowledgeService.updateById(knowledge);
        if(b){
            return success("修改成功！交予管理员审核");
        }else{
            return success("修改失败！");
        }

    }

    @PostMapping("/queryKnowledgeById")
    public AjaxResult queryKnowledgeById(@RequestBody Knowledge knowledge)
    {
        Knowledge model = knowledgeService.getById(knowledge.getId());
        return success(model);

    }




}
